package com.woupla.navigation;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

import javax.enterprise.event.Observes;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

import org.jboss.seam.faces.event.PostLoginEvent;
import org.jboss.seam.faces.event.PreLoginEvent;
import org.jboss.seam.faces.security.LoginListener;

import com.ocpsoft.pretty.PrettyContext;
import com.ocpsoft.pretty.faces.config.mapping.UrlMapping;
import com.ocpsoft.pretty.faces.url.URL;
import com.ocpsoft.pretty.faces.util.PrettyURLBuilder;

public class WouplaLoginListener extends LoginListener {

	private static final String PRE_LOGIN_URL = LoginListener.class.getName() + "_PRE_LOGIN_URL";

	public void observePreLoginEvent(@Observes PreLoginEvent event) {
		if (event.getFacesContext().getExternalContext().getRequest() instanceof HttpServletRequest) {
			HttpServletRequest request = (HttpServletRequest) event.getFacesContext().getExternalContext().getRequest();
			StringBuffer sb = request.getRequestURL();
			// build the querystring out of the request parameters, because
			// Reqeust#getQueryString is often null
			Map<String, String> requestParameterMap = event.getFacesContext().getExternalContext().getRequestParameterMap();
			if (requestParameterMap != null) {
				boolean first = true;
				for (Map.Entry<String, String> entry : requestParameterMap.entrySet()) {
					if (first) {
						sb.append("?");
					} else {
						sb.append("&");
					}
					sb.append(entry.getKey()).append("=").append(entry.getValue());
				}
			}
			String requestedUrl = sb.toString();
			event.getSessionMap().put(PRE_LOGIN_URL, requestedUrl);
		}
	}

	public void observePostLoginEvent(@Observes PostLoginEvent event) {
		FacesContext context = event.getFacesContext();
		HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
		PrettyContext prettyContext = PrettyContext.getCurrentInstance(request);
		PrettyURLBuilder builder = new PrettyURLBuilder();

		if (context.getExternalContext().getSessionMap().get(PRE_LOGIN_URL) != null) {
			String oldUrl = (String) context.getExternalContext().getSessionMap().get(PRE_LOGIN_URL);
			context.getExternalContext().getSessionMap().remove(PRE_LOGIN_URL);
			UrlMapping mapping = prettyContext.getConfig().getMappingForUrl(new URL(oldUrl));
			String targetURL = builder.build(mapping, true, new ArrayList());
			try {
				event.getFacesContext().getExternalContext().redirect(targetURL);
				// event.getFacesContext().getExternalContext().redirect(oldUrl+"?faces-redirect=true");
			} catch (IOException ex) {
				throw new RuntimeException(ex);
			}
		}else{
			String sb = request.getRequestURL().toString();
			sb = sb.substring(0, sb.lastIndexOf("/"));
			sb = sb.substring(0, sb.lastIndexOf("/"));
			UrlMapping mapping = prettyContext.getConfig().getMappingById("welcome");
			String targetURL = builder.build(mapping, true, new ArrayList());
			targetURL = sb + targetURL;
			try {
				event.getFacesContext().getExternalContext().redirect(targetURL);
				// event.getFacesContext().getExternalContext().redirect(oldUrl+"?faces-redirect=true");
			} catch (IOException ex) {
				throw new RuntimeException(ex);
			}
		}
		
	}

}
